//
//  ToolManager.swift
//  daily_count
//
//  Created by 段帅 on 2018/3/28.
//  Copyright © 2018年 Sooc. All rights reserved.
//工具类管理  网络请求类

import UIKit
import SVProgressHUD
class DSToolManager: NSObject {
    //普通post请求
    class func post(URLString: String,parameters: [String: AnyObject]?, completed:@escaping ((_ json: AnyObject?, _ isSuccess: Bool)->())){
        
        HttpRequest.shareManager().request(Method: .POST, URLString: URLString, parameters: parameters , completed: completed);
        
    }
    //带token 的post请求
    class func postWithToken(URLString: String,parameters: [String: AnyObject]?, completed:@escaping ((_ json: AnyObject?, _ isSuccess: Bool)->())){
        
        var para = [String: AnyObject]();
        if (parameters != nil) {
             para = parameters!;
        }
//        para.updateValue(UserDefaults.standard.object(forKey: COUNT) as AnyObject, forKey: "count");
        para.updateValue(UserDefaults.standard.object(forKey: TOKEN) as AnyObject, forKey: "token");
        para.updateValue(UserDefaults.standard.object(forKey: TOKEN_SC) as AnyObject, forKey: "token_secret");
        print("url--\(URLString) paras--\(para)");
        
        HttpRequest.shareManager().request(Method: .POST, URLString: URLString, parameters: para, completed: completed);
        
    }
    //普通get请求
    class func get(URLString: String,parameters: [String: AnyObject]?, completed:@escaping ((_ json: AnyObject?, _ isSuccess: Bool)->())){
        
         HttpRequest.shareManager().request(Method: .GET, URLString: URLString, parameters: parameters, completed: completed);
        
    }
    
    
    
    class func uploadImage(URLString: String,parameters: [String: AnyObject]?, image : UIImage ,completed:@escaping ((_ json: AnyObject?, _ isSuccess: Bool)->())){
        
        var para = [String: AnyObject]();
        if (parameters != nil) {
            para = parameters!;
        }
        //        para.updateValue(UserDefaults.standard.object(forKey: COUNT) as AnyObject, forKey: "count");
        para.updateValue(UserDefaults.standard.object(forKey: TOKEN) as AnyObject, forKey: "token");
        para.updateValue(UserDefaults.standard.object(forKey: TOKEN_SC) as AnyObject, forKey: "token_secret");
        print("url--\(URLString) paras--\(para)");
        
        HttpRequest.shareManager().request2(URLString: URLString, image: image, parameters: para, completed: completed);
        
    }
    
    
    
    //网络提示
    class func alertSucess(message:String){
        SVProgressHUD.showSuccess(withStatus: message);
    }
    class func alertFaild(message:String){
        SVProgressHUD.showError(withStatus: message);
    }
    
    //延时操作
    typealias Task = (_ cancel : Bool) -> Void
    
    class func delay(_ time: TimeInterval, task: @escaping ()->()) ->  Task? {
        
        func dispatch_later(block: @escaping ()->()) {
            let t = DispatchTime.now() + time
            DispatchQueue.main.asyncAfter(deadline: t, execute: block)
        }
        var closure: (()->Void)? = task
        var result: Task?
        
        let delayedClosure: Task = {
            cancel in
            if let internalClosure = closure {
                if (cancel == false) {
                    DispatchQueue.main.async(execute: internalClosure)
                }
            }
            closure = nil
            result = nil
        }
        
        result = delayedClosure
        
        dispatch_later {
            if let delayedClosure = result {
                delayedClosure(false)
            }
        }
        return result
    }
    
    func cancel(_ task: Task?) {
        task?(true)
    }
    
    
    //空白页
    class func blank(string:String?) -> UIView {
        let view = UIView();
        view.frame = CGRect.init(x: 0, y: 0, width: SIZE_WITH, height: SIZE_HEIGH);
        view.backgroundColor = UIColor.groupTableViewBackground;
        let image = UIImageView.init(frame: CGRect.init(x: SIZE_WITH/2-34, y: 100, width: 68, height: 84));
        image.image = UIImage.init(named: "空白页");
        view.addSubview(image);
        
        let label = UILabel.init(frame: CGRect.init(x: 0, y: image.frame.maxY+10, width: SIZE_WITH, height: 20));
        label.font = UIFont.systemFont(ofSize: 14);
        label.textAlignment = NSTextAlignment.center;
        label.text = string ;
        view.addSubview(label);
        return view;
    }
    
    

}
